import { Remote, wrap } from "comlink";
import { Pool } from "../../../package/work-pool/pool";

export class WorkerPool<T> extends Pool<{ worker: Worker; module: Remote<T> }> {
    constructor(originWorker: new () => Worker, min = 3, coolTime = 0) {
        super(
            () => {
                const worker = new originWorker();
                return {
                    id: new Date().getTime(),
                    worker,
                    module: wrap<T>(worker),
                };
            },
            (T) => {
                T.worker.terminate();
            },
            2000,
            min,
            coolTime
        );
    }
}
